#include <gtest/gtest.h>
#include "../mqserver/mq_exchange.hpp"

csjmq::ExchangeManager::ptr emp;
class Exchangetest : public testing::Environment
{
    virtual void SetUp() override
    {
        emp = std::make_shared<csjmq::ExchangeManager>("./data/test.db");
    }

    virtual void TearDown() override
    {
        emp->clear();
        std::cout << "最后的清理工作" << std::endl;
    }
};

TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};
    emp->declareExchange("exchange1", csjmq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("exchange2", csjmq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("exchange3", csjmq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("exchange4", csjmq::ExchangeType::DIRECT, true, false, map);
    ASSERT_EQ(emp->size(), 4);
}
TEST(exchange_test, select_test)
{
    csjmq::Exchange::ptr exp = emp->selectExchange("exchange2");
    ASSERT_NE(exp.get(), nullptr);
    ASSERT_EQ(exp->name, "exchange2");
    ASSERT_EQ(exp->durable, true);
    ASSERT_EQ(exp->auto_delete, false);
    ASSERT_EQ(exp->type, csjmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->getArgs(), std::string("k1=v1&k2=v2&"));
}

TEST(exchange_test, delete_test)
{
    emp->deleteExchange("exchange2");
    ASSERT_EQ(emp->exists("exchange2"), false);
}
TEST(exchange_test, exists_test)
{
    ASSERT_EQ(emp->exists("exchange3"), true);
}

int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new Exchangetest);
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}
